---
{{ $grafanaVersion := "10.2.2" }}
{{ $bundledPlugins := "petrslavotinek-carpetplot-panel,vonage-status-panel,btplc-status-dot-panel,natel-plotly-panel,savantly-heatmap-panel,grafana-piechart-panel,grafana-worldmap-panel" }}
---
artifact: {{ $.ModuleName }}/{{ $.ImageName }}-src-files
from: {{ $.Images.BASE_ALPINE }}
git:
- add: /{{ $.ModulePath }}modules/300-{{ $.ModuleName }}/images/{{ $.ImageName }}/patches
  to: /patches
  includePaths:
  - '**/*.patch'
  stageDependencies:
    install:
    - '**/*.patch'
shell:
  beforeInstall:
  - apk add --no-cache git patch
  install:
  - mkdir -p /usr/src/app
  - cd /usr/src/app
  - git clone --depth 1 --branch v{{ $grafanaVersion }} {{ $.SOURCE_REPO }}/grafana/grafana.git .
  - find /patches -name '*.patch' -exec git apply {} \;
---
artifact: {{ $.ModuleName }}/{{ $.ImageName }}-js-builder
from: {{ $.Images.BASE_NODE_16_ALPINE }}
import:
- artifact: {{ $.ModuleName }}/{{ $.ImageName }}-src-files
  add: /usr/src/app
  to: /usr/src/app
  before: install
shell:
  beforeInstall:
  - apk add --no-cache git
  install:
  - cd /usr/src/app
  - yarn install
  - export NODE_ENV=production NODE_OPTIONS="--max_old_space_size=8000"
  - yarn build
---
artifact: {{ $.ModuleName }}/{{ $.ImageName }}-go-builder
from: {{ $.Images.BASE_GOLANG_21_BULLSEYE }}
mount:
- fromPath: ~/go-pkg-cache
  to: /go/pkg
import:
- artifact: {{ $.ModuleName }}/{{ $.ImageName }}-src-files
  add: /usr/src/app
  to: /usr/src/app
  before: install
shell:
  beforeInstall:
  - apt-get update
  - apt-get -y --no-install-recommends install git gcc musl musl-tools
  install:
  - export GOPROXY={{ $.GOPROXY }} CGO_ENABLED=1 CGO_LDFLAGS="-static" GOOS=linux GOARCH=amd64 CC=/usr/bin/musl-gcc
  - cd /usr/src/app
  - go install github.com/google/wire/cmd/wire@v0.5.0
  - /go/bin/wire gen -tags oss ./pkg/server
  - go build -ldflags '-w -X main.version={{ $grafanaVersion }} -linkmode external' -o ./bin/linux-amd64/grafana ./pkg/cmd/grafana
---
artifact: {{ $.ModuleName }}/{{ $.ImageName }}-entrypoint
from: {{ $.Images.BASE_GOLANG_20_ALPINE }}
mount:
- fromPath: ~/go-pkg-cache
  to: /go/pkg
git:
- add: /{{ $.ModulePath }}modules/300-{{ $.ModuleName }}/images/{{ $.ImageName }}/entrypoint
  to: /app
  stageDependencies:
    install:
    - '**/*'
shell:
  install:
  - cd /app
  - export GOPROXY={{ $.GOPROXY }} CGO_ENABLED=0 GOOS=linux GOARCH=amd64
  - go build -ldflags="-s -w" -o entrypoint main.go
  - chown -R 64535:64535 /app/
  - chmod 0700 /app/entrypoint
---
artifact: {{ $.ModuleName }}/{{ $.ImageName }}-grafana-distr
from: {{ $.Images.BASE_UBUNTU }}
import:
- artifact: {{ $.ModuleName }}/{{ $.ImageName }}-go-builder
  add: /usr/src/app/bin/linux-amd64
  to: /usr/share/grafana/bin
  before: install
  includePaths:
  - grafana
- artifact: {{ $.ModuleName }}/{{ $.ImageName }}-js-builder
  add: /usr/src/app/public
  to: /usr/share/grafana/public
  before: install
- artifact: {{ $.ModuleName }}/{{ $.ImageName }}-js-builder
  add: /usr/src/app/tools
  to: /usr/share/grafana/tools
  before: install
- artifact: {{ $.ModuleName }}/{{ $.ImageName }}-src-files
  add: /usr/src/app/conf
  to: /usr/share/grafana/conf
  before: install
git:
- add: /{{ $.ModulePath }}modules/300-{{ $.ModuleName }}/images/{{ $.ImageName }}/grafana_home_dashboard.json
  to: /usr/share/grafana/public/dashboards/grafana_home_dashboard.json
  stageDependencies:
    install:
    - '**/*'
- add: /{{ $.ModulePath }}modules/300-{{ $.ModuleName }}/images/{{ $.ImageName }}/web
  to: /img/
  stageDependencies:
    install:
    - '**/*'
shell:
  beforeInstall:
  - apt-get update
  - apt-get -y --no-install-recommends install libfontconfig curl ca-certificates openssl unzip git
  - apt-get clean
  - apt-get autoremove -y
  - rm -rf /var/lib/apt/lists/*
  install:
  - |
    export PATH="/usr/share/grafana/bin:$PATH" \
    GF_PATHS_CONFIG="/etc/grafana/grafana.ini" \
    GF_PATHS_DATA="/var/lib/grafana" \
    GF_PATHS_HOME="/usr/share/grafana" \
    GF_PATHS_LOGS="/var/log/grafana" \
    GF_PATHS_PLUGINS="/usr/share/grafana/plugins-bundled" \
    GF_PATHS_PROVISIONING="/etc/grafana/provisioning"
  - cd $GF_PATHS_HOME
  - |
    mkdir -p "$GF_PATHS_HOME/.aws" \
             "$GF_PATHS_PROVISIONING/datasources" \
             "$GF_PATHS_PROVISIONING/dashboards" \
             "$GF_PATHS_PROVISIONING/notifiers" \
             "$GF_PATHS_PROVISIONING/plugins" \
             "$GF_PATHS_PROVISIONING/access-control" \
             "$GF_PATHS_LOGS" \
             "$GF_PATHS_PLUGINS" \
             "$GF_PATHS_DATA"
  - cp "$GF_PATHS_HOME/conf/sample.ini" "$GF_PATHS_CONFIG"
  - cp "$GF_PATHS_HOME/conf/ldap.toml" /etc/grafana/ldap.toml
  - cp /img/* "$GF_PATHS_HOME/public/img"
  - chmod -R 777 "$GF_PATHS_DATA" "$GF_PATHS_HOME/.aws" "$GF_PATHS_LOGS" "$GF_PATHS_PLUGINS" "$GF_PATHS_PROVISIONING" "$GF_PATHS_HOME"
  - |
    IFS="," && \
    BUNDLED_PLUGINS={{ $bundledPlugins }} && \
    for plugin in ${BUNDLED_PLUGINS}; do \
      grafana cli --pluginsDir "${GF_PATHS_PLUGINS}" plugins install ${plugin}; \
    done
  - chmod +r /etc/grafana/grafana.ini
  - export BUNDLED_PLUGINS_PATH="${GF_PATHS_PLUGINS}"
  - chown -R 64535:64535 /usr/share/grafana
  - chown -R 64535:64535 /etc/grafana
  - chown -R 64535:64535 /var/lib/grafana
  - chown -R 64535:64535 /var/log/grafana
  - chmod 0700 ./bin/grafana
---
{{ $binariesList := "/usr/lib64/* /usr/lib/*" }}
---
artifact: {{ $.ModuleName }}/{{ $.ImageName }}-binaries-artifact
from: {{ $.Images.BASE_ALT_DEV }}
shell:
  beforeSetup:
  - apt-get update
  - apt-get install -y glibc
  install:
    - /binary_replace.sh -i "{{ $binariesList }}" -o /relocate
---
image: {{ $.ModuleName }}/{{ $.ImageName }}
fromImage: common/distroless
import:
- artifact: {{ $.ModuleName }}/{{ $.ImageName }}-binaries-artifact
  add: /relocate
  to: /
  before: install
  includePaths:
  - '**/*'
- artifact: {{ $.ModuleName }}/{{ $.ImageName }}-entrypoint
  add: /app/entrypoint
  to: /usr/local/bin/entrypoint
  before: install
- artifact: {{ $.ModuleName }}/{{ $.ImageName }}-grafana-distr
  add: /usr/share/grafana/
  to: /usr/share/grafana/
  before: install
- artifact: {{ $.ModuleName }}/{{ $.ImageName }}-grafana-distr
  add: /etc/grafana/
  to: /etc/grafana/
  before: install
- artifact: {{ $.ModuleName }}/{{ $.ImageName }}-grafana-distr
  add: /var/lib/grafana/
  to: /var/lib/grafana/
  before: install
- artifact: {{ $.ModuleName }}/{{ $.ImageName }}-grafana-distr
  add: /var/log/grafana/
  to: /var/log/grafana/
  before: install
docker:
  ENV:
    PATH: "/usr/share/grafana/bin:$PATH"
    GF_PATHS_CONFIG: "/etc/grafana/grafana.ini"
    GF_PATHS_DATA: "/var/lib/grafana"
    GF_PATHS_HOME: "/usr/share/grafana"
    GF_PATHS_LOGS: "/var/log/grafana"
    GF_PATHS_PLUGINS: "/usr/share/grafana/plugins-bundled"
    GF_PATHS_PROVISIONING: "/etc/grafana/provisioning"
    BUNDLED_PLUGINS_PATH: "${GF_PATHS_PLUGINS}"
  WORKDIR: $GF_PATHS_HOME
  EXPOSE:
  - "3000"
  ENTRYPOINT:
  - "entrypoint"
